package org.jeecg.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.system.entity.SysRegion;
import org.jeecg.modules.system.entity.SysUserRegion;
import org.jeecg.modules.system.mapper.SysUserRegionMapper;
import org.jeecg.modules.system.model.RegionIdModel;
import org.jeecg.modules.system.service.ISysRegionService;
import org.jeecg.modules.system.service.ISysUserRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class SysUserRegionServiceImpl extends ServiceImpl<SysUserRegionMapper, SysUserRegion> implements ISysUserRegionService {
    @Autowired
    private ISysRegionService sysRegionService;
//@Autowired
//private YksRegionMapper yksRegionMapper;

    /**
     * 根据用户id查询部门信息
     */
    @Override
    public List<RegionIdModel> querySysRegionIdsOfUser(String userId) {
        LambdaQueryWrapper<SysUserRegion> queryUserRegion = new LambdaQueryWrapper<SysUserRegion>();
        LambdaQueryWrapper<SysRegion> queryRegion = new LambdaQueryWrapper<SysRegion>();
        try {
            queryUserRegion.eq(SysUserRegion::getUserId, userId);
            List<String> regionIdList = new ArrayList<>();
            List<RegionIdModel> regionIdModelList = new ArrayList<>();
            List<SysUserRegion> userRegionList = this.list(queryUserRegion);
            if(userRegionList != null && userRegionList.size() > 0) {
                for(SysUserRegion userRegion : userRegionList) {
                    regionIdList.add(userRegion.getRegionId());
                }
                queryRegion.in(SysRegion::getId, regionIdList);
                List<SysRegion> depList = sysRegionService.list(queryRegion);
                //jeecg-boot/issues/3906
                if(depList != null && depList.size() > 0) {
                    for(SysRegion region : depList) {
                        regionIdModelList.add(new RegionIdModel().convertByUserRegion(region));
                    }
                }
                return regionIdModelList;
            }
        }catch(Exception e) {
            e.fillInStackTrace();
        }
        return null;


    }
}
